package cn.tedu.zx.product.webapi.controller;


import cn.tedu.zx.pojo.court.dto.AlbumAddNewDto;
import cn.tedu.zx.pojo.court.dto.AlbumUpdateDto;
import cn.tedu.zx.pojo.court.vo.AlbumDetailsVO;
import cn.tedu.zx.pojo.court.vo.AlbumListVO;
import cn.tedu.zx.pojo.court.vo.PageData;
import cn.tedu.zx.product.service.AlbumService;
import cn.tedu.zx.product.webapi.ex.ServiceCode;
import cn.tedu.zx.product.webapi.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Slf4j
@RestController
@RequestMapping("/album")
//通过knife4j配置IPI文档
@Api(tags = "01.相册模块")
@Validated/*未封装参数检查注解*/
public class AlbumController {
    @Autowired
    private AlbumService service;

    /*本类统一处理异常,所有方法抛出异常统一处理 *//*
    @ExceptionHandler
    public String handlerExction(ServiceException e){
        return e.getMessage();
    }*/

    //    @RequestMapping(value = "/addnew",method = {RequestMethod.POST,RequestMethod.GET})
    @PostMapping("/addNew")
    @ApiOperation("添加相册")
    @ApiOperationSupport(order = 100)
    /*@ApiIgnore注解忽略注解后的请求参数*/
    public JsonResult addNew(@Valid AlbumAddNewDto albumAddNewDto) {
        log.debug("开始执行{}", albumAddNewDto);
        service.addNew(albumAddNewDto);
        log.debug("添加完成");
//        JsonResult jsonResult = new JsonResult();
//        jsonResult.setState(0);
//        jsonResult.setMessage("添加成功");
//        return jsonResult;
        /*方法二*/
//        return new JsonResult().setState(0).setMessage("添加成功");//链式写法
        /*方法三*/
        return JsonResult.success();//静态方法
    }

    @PostMapping("/delete")
    @ApiOperation("根据id删除")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams(
            {@ApiImplicitParam(name = "albumId", value = "相册id", required = true, dataType = "Long")
                    /*@ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "Long")*/}
    )
    public JsonResult delete(@Range(min = 1, message = "Id异常，请添加正确的id") @RequestParam Long albumId
            /*@NotNull @RequestParam Long userId*/) {
//        "".substring(3);
//        JsonResult jsonResult = new JsonResult();
//        jsonResult.setState(ServiceCode.FAIL.getValue());
//        jsonResult.setMessage("成功删除");
//        return jsonResult;
        service.deleteById(albumId);
        return JsonResult.fail(ServiceCode.OK, "成功删除");
    }

    @GetMapping("/selectDetails")
    @ApiOperation("根据id查询相册详情")
    @ApiOperationSupport(order = 500)
    @ApiImplicitParams(
            {@ApiImplicitParam(name = "albumId", value = "相册id", required = true, dataType = "Long")}
    )
    public JsonResult selectByI(@Range(min = 1, message = "Id异常，请添加正确的id") @RequestParam Long albumId) {
        AlbumDetailsVO albumDetailsVO = service.selectDetailsById(albumId);
        return JsonResult.success(albumDetailsVO);
    }

    @PostMapping("/update")
    @ApiOperation("修改相册")
    @ApiOperationSupport(order = 200)
    public JsonResult update(@Valid AlbumUpdateDto updateDto) {
        service.updateById(updateDto);
//        throw new RuntimeException("出现空指针异常");
        return JsonResult.success();
    }

    @GetMapping("/list")
    @ApiOperation("相册列表")
    @ApiOperationSupport(order = 600)
    @ApiImplicitParam(name = "pageNum", value = "相册页码", required = true, dataType = "Long")
    public JsonResult list(@RequestParam Integer pageNum) {

        if (pageNum == null) {
            pageNum = 1;
        }
        log.debug("开始处理请求{}", pageNum);
        PageData<AlbumListVO> pageData = service.list(pageNum);
        log.debug("查询完成{}", pageData);
        return JsonResult.success().setData(pageData);
    }

}
